package com.cpit.platform.schedule;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.cpit.platform.config.UploadPathConfig;
import com.cpit.platform.log.LogOperator;

@Configuration
@EnableScheduling // 启用定时任务
public class SchedulingConfig {
	
	private static final String loggerName = SchedulingConfig.class.getName();
	
	@Autowired
	private UploadPathConfig uploadPath;
	
	//@Scheduled(cron = "0 0 1 * * ?") //每天1时清空临时目录
    public void scheduler() {
		Path dir = Paths.get(uploadPath.getTemp());
		
		try {
			DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
		    for(Path path : stream){
		        LogOperator.debug(loggerName, "del file :"+ path.getFileName());
		        Files.deleteIfExists(path);
		    }
			
		} catch (IOException e) {
			LogOperator.err(loggerName, "delete ./upload/temp/*.* file error!", e);
		}
    }
}
